package com.bos.service.impl;

import com.bos.dao.repositories.WayBillRepository;
import com.bos.dao.search.WayBillSearchRepository;
import com.bos.domian.base.WayBill;
import com.bos.service.WayBillSearchService;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author misterWei
 * @create 2018年11月27号:20点43分
 * @mailbox mynameisweiyan@gmail.com
 */
@SuppressWarnings("all")
@Service
@Transactional(rollbackFor = {NullPointerException.class, ParseException.class,NumberFormatException.class})
public class WayBillSearchServiceImpl implements WayBillSearchService {
    @Autowired
    private WayBillSearchRepository wayBillSearchRepository;
    @Autowired
    private WayBillRepository wayBillRepository;
    @Override
    public Map<String, Object> findAll(Pageable pageable,WayBill wayBill) {
        //这个语句用来组合查询 可以是 and (must)    or(should)   and not  相当于条件必须不成立(must not)
        BoolQueryBuilder  query = new BoolQueryBuilder();
        if (StringUtils.isNotBlank(wayBill.getWayBillNum())) {
            QueryBuilder queryWayBillNum = new TermQueryBuilder("wayBillNum",wayBill.getWayBillNum());
            query.must(queryWayBillNum);
        }
        if (StringUtils.isNotBlank(wayBill.getSendAddress())){
            QueryBuilder querySendAddress = new TermQueryBuilder("sendAddress",wayBill.getSendAddress());
            query.must(querySendAddress);

        }
        if (StringUtils.isNotBlank(wayBill.getRecAddress())){
            //收货地模糊查询
            QueryBuilder queryRecAddress = new WildcardQueryBuilder("recAddress","*"+wayBill.getRecAddress()+"*");
            query.must(queryRecAddress);
        }
        if (StringUtils.isNotBlank(wayBill.getSendProNum())){
            QueryBuilder querySendProNum = new TermQueryBuilder("sendProNum",wayBill.getSendProNum());
            query.must(querySendProNum);
        }
        if (StringUtils.isNotBlank(wayBill.getRecAddress()) && wayBill.getSignStatus()!=0){
            QueryBuilder querySignStatus = new TermQueryBuilder("signStatus",wayBill.getSignStatus());
            query.must(querySignStatus);
        }
        //有问题
        Page<WayBill> all = wayBillSearchRepository.search(query,pageable);
        Map<String,Object> wayBillList  = new HashMap<>();
        wayBillList.put("total",all.getTotalElements());
        wayBillList.put("rows",all.getContent());
        wayBillList.put("waybillList",all.getContent());
        return wayBillList;
    }

    @Override
    public void syncWayBill() {
        List<WayBill> wayBillList = wayBillRepository.findAll();
        wayBillSearchRepository.save(wayBillList);
    }
}
